<script setup>
import { User, Lock } from '@element-plus/icons-vue'
import { ref } from 'vue'
import { ElMessage } from 'element-plus'

// 控制注册与登录表单的显示，默认显示注册
const isRegister = ref(false)

// 定义数据模型
const registerData = ref({
    username: '',
    password: '',
    rePassword: ''
})

// 校验密码的函数
const checkRePassword = (rule, value, callback) => {
    if (value === '') {
        callback(new Error('请再次确认密码'))
    } else if (value !== registerData.value.password) {
        callback(new Error('请确保两次输入的密码一样'))
    } else {
        callback()
    }
}

// 定义表单校验规则
const rules = {
    username: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 5, max: 16, message: '长度为5~16位非空字符', trigger: 'blur' }
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 5, max: 16, message: '长度为5~16位非空字符', trigger: 'blur' }
    ],
    rePassword: [
        { validator: checkRePassword, trigger: 'blur' }
    ]
}


//登录
import { userLoginService,getUserPermissionService,getUserInfoService } from '../api/login/login.js'
import { useTokenStore } from '@/stores/token.js'
import { useUserPermissionStore } from '@/stores/userPermission.js'
import {useRouter} from 'vue-router'
import {useUserInfoStore} from '@/stores/userinfo.js'



const router = useRouter()
const tokenStore = useTokenStore()
const PermissionStore = useUserPermissionStore()
const userStore = useUserInfoStore()
const login = async () =>{
    let result = await userLoginService(registerData.value)
    console.log(result.data)
    tokenStore.setToken(result.data.accessToken)
    const PermissionInfo = await getUserPermissionService()
    console.log(PermissionInfo.data)
    PermissionStore.setPermission(PermissionInfo.data)
    ElMessage.success(result.msg ? result.msg : '登录成功')
    router.push('/')
}

// 定义函数,清空数据模型的数据
const clearRegisterData = () => {
    registerData.value = {
        username: '',
        password: '',
        rePassword: ''
    }
}
</script>

<template>
    <div class="login-page">
        <div class="form-container">
            <el-card class="box-card">
                <!-- 注册表单 -->
                <el-form v-if="isRegister" ref="form" size="large" autocomplete="off" :model="registerData" :rules="rules">
                    <el-form-item>
                        <h1>注册</h1>
                    </el-form-item>
                    <el-form-item prop="username">
                        <el-input :prefix-icon="User" placeholder="请输入用户名" v-model="registerData.username"></el-input>
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input :prefix-icon="Lock" type="password" placeholder="请输入密码" v-model="registerData.password"></el-input>
                    </el-form-item>
                    <el-form-item prop="rePassword">
                        <el-input :prefix-icon="Lock" type="password" placeholder="请输入再次密码" v-model="registerData.rePassword"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button class="button" type="primary" auto-insert-space @click="register">注册</el-button>
                    </el-form-item>
                    <el-form-item class="flex">
                        <el-link type="info" :underline="false" @click="isRegister = false;clearRegisterData()">
                            ← 返回
                        </el-link>
                    </el-form-item>
                </el-form>
                <!-- 登录表单 -->
                <el-form v-else ref="form" size="large" autocomplete="off" :model="registerData" :rules="rules">
                    <el-form-item>
                        <h1>登录</h1>
                    </el-form-item>
                    <el-form-item prop="username">
                        <el-input :prefix-icon="User" placeholder="请输入用户名" v-model="registerData.username"></el-input>
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input :prefix-icon="Lock" type="password" placeholder="请输入密码" v-model="registerData.password"></el-input>
                    </el-form-item>
                    <el-form-item class="flex">
                        <div class="flex">
                            <el-checkbox>记住我</el-checkbox>
                            <el-link type="primary" :underline="false">忘记密码？</el-link>
                        </div>
                    </el-form-item>
                    <el-form-item>
                        <el-button class="button" type="primary" auto-insert-space @click="login">登录</el-button>
                    </el-form-item>
                    <el-form-item class="flex">
                        <el-link type="info" :underline="false" @click="isRegister = true;clearRegisterData()">
                            注册 →
                        </el-link>
                    </el-form-item>
                </el-form>
            </el-card>
        </div>
    </div>
</template>

<style lang="scss" scoped>
.login-page {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #f5f5f5;
    /* 如果需要背景图片，取消下面的注释 */
    /* background-image: url('@/assets/login_bg.png'); */
    /* background-size: cover; */
    /* background-position: center; */
    margin: 0;
    padding: 0;
    overflow: hidden;
}

.form-container {
    width: 100%;
    max-width: 400px;
    padding: 20px;
    box-sizing: border-box;
}

.box-card {
    width: 100%;
    padding: 20px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
    border-radius: 8px;
    background: #fff;
}

.button {
    width: 100%;
}

.flex {
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

h1 {
    text-align: center;
    margin-bottom: 20px;
    font-size: 24px;
    color: #333;
}
</style>